<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .dustbin {
            width: 100px;
            height: 260px;
            line-height: 1.4;
            background-color: gray;
            font-size: 36px;
            font-family: "微软雅黑", "Yahei Mono";
            text-align: center;
            text-shadow: -1px -1px #bbb;
            float: left;
        }
        .dragbox {
            width: 500px;
            padding-left: 20px;
            float: left;
        }
        .draglist {
            padding: 10px;
            margin-bottom: 5px;
            border: 2px dashed #ccc;
            background-color: #eee;
            cursor: move;
        }

        .dragremind {
            padding-top: 2em;
            clear: both;
        }
    </style>
</head>
<body>
    <div class="dustbin"><br />垃<br />圾<br />箱</div>
    <div class="dragbox">
        <div class="draglist" title="拖拽我" draggable="true" >列表1</div>
        <div class="draglist" title="拖拽我" draggable="true">列表2</div>
        <div class="draglist" title="拖拽我" draggable="true">列表3</div>
        <div class="draglist" title="拖拽我" draggable="true">列表4</div>
        <div class="draglist" title="拖拽我" draggable="true">列表5</div>
        <div class="draglist" title="拖拽我" draggable="true">列表6</div>
    </div>
    <div class="dragremind"></div>

    <script>
        // 源对象， 被拖拽的对象
        var draglists = document.querySelectorAll('.draglist');
        //目标对象
        var dustbin = document.querySelector('.dustbin');
        var eleDom = null; //当前被拖拽的对象
        for(let i =0,len =draglists.length; i<len;i++) {
            draglists[i].ondragstart = function(ev){
                //开始拖拽
                ev.dataTransfer.setData('text',ev.target.innerHTML);
                
                var imgDom = new Image();
                imgDom.src = './imgs/play-btn.png';
                // ev.dataTransfer.setDragImage(imgDom,0,0);
                ev.dataTransfer.setDragImage(ev.target,0,0);
                eleDom = this;
            }
            draglists[i].ondragend = function(ev){
                //清除数据
                ev.dataTransfer.clearData('text');
                eleDom = null;
            }


            //目标对象
            dustbin.ondragenter = function(ev){
                // 当被鼠标拖动的对象进入其容器范围内时触发此事件
                this.style.color='#fff';
            }

            dustbin.ondragover = function(ev){
                ev.preventDefault();
            }
            dustbin.ondrop = function(ev){
                if(eleDom){
                    var info = ev.dataTransfer.getData('text');
                    document.querySelector('.dragremind').innerHTML = info + '被扔进了垃圾箱'
                    eleDom.remove();
                    // eleDom.preventNode.removeChild(eleDom); //复杂
                }
            }

        }
    </script>
</body>
</html>